﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class NotePlan : BasePage
    {
        nm_noteplan_Model model1 = new nm_noteplan_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (nm_noteplan_Model)Session["NotePlanModel"];
                BindDrop();
                LoadList();
                lbl_Message.Text = "";
            }
        }

        /// <summary>
        /// 绑定模板
        /// </summary>
        public void BindDrop()
        {
            FillDropDownList("GetDrop1", "ID", "MoubleName", "请选择模板", this.FindDropArea);
        }

        /// <summary>
        /// 绑定表
        /// </summary>
        public void LoadList()
        {
            Busi_nm_noteplan users = new Busi_nm_noteplan();
            DataTable dt = users.GetList(txt_FindUser.Text.Trim(), int.Parse(FindDropArea.SelectedValue));
            Grd_User.DataSource = dt;
            Grd_User.DataBind();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Grd_User.EditIndex = e.NewEditIndex;
            LoadList();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Grd_User.EditIndex = -1;
            LoadList();
        }

        /// <summary>
        /// 选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadList();
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            LoadList();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
            try
            {
                Busi_nm_noteplan.Delete(ID);
                MessageBox.ShowDialog(Page, "删除成功！", "success");
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "删除失败！" + ex.Message, "error");
                LoadList();
            }
        }

        /// <summary>
        /// 填充值到控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList ddlEarth = new DropDownList();
            ddlEarth = (DropDownList)e.Row.FindControl("ddlEarth");
            if (ddlEarth != null)
            {
                FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", ddlEarth);
                int Id = Convert.ToInt32(Grd_User.DataKeys[e.Row.RowIndex].Values[0].ToString());
                Busi_nm_noteplan u = new Busi_nm_noteplan();
                nm_noteplan_Model model = u.GetListModel(Id);
                ddlEarth.SelectedValue = model.moubleID.ToString();
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                sys_user_Model user = new sys_user_Model ();
                nm_noteplan_Model model = new nm_noteplan_Model();
                model.PlanId = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
                model.PlanName = ((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtplanname")).Text.Trim();
                model.moubleID = Convert.ToInt32(((DropDownList)Grd_User.Rows[e.RowIndex].FindControl("ddlEarth")).SelectedValue);
                model.PlanExplain = ((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtplanexplain")).Text.Trim();
                model.NoteContent = ((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtnotecontent")).Text.Trim();
                model.SendTime = Convert.ToDateTime(((TextBox)Grd_User.Rows[e.RowIndex].FindControl("txtsendtime")).Text.Trim());
                model.UpdatePersonID = user.UserID;
                model.UpdateTime = DateTime.Now;
                if (model.PlanName == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写短信发送名称！", "error");
                    return;
                }
                if (model.moubleID.ToString() == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写计划发送模板！", "error");
                    return;
                }
                if (model.PlanExplain == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写计划发送说明！", "error");
                    return;
                }
                if (model.NoteContent == "")
                {
                    MessageBox.ShowDialog(Page, "请填填写计划发送内容！", "error");
                    return;
                }
                if (model.SendTime==null)
                {
                    MessageBox.ShowDialog(Page, "请填填写计划发送时间！", "error");
                    return;
                }
                else
                {
                    new Busi_nm_noteplan().Update(model);
                    MessageBox.ShowDialog(Page, "修改成功！", "success");
                }
                Grd_User.EditIndex = -1;
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "修改失败！" + ex.Message, "error");
            }
        }
    }
}
       